<?php

namespace app\model\v1\recovery;

use app\model\v1\recruit\RecruitPersonageAuthentication;
use crmeb\traits\ModelTrait;
use think\Model;


class RecoveryRegAuth extends Model
{
    use ModelTrait;


    protected $updateTime = false;
    protected $createTime = false;


    /**
     * 获取用户是否已认证
     * @param int $uid
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getUserIsAuth(int $uid)
    {
        $find = RecruitPersonageAuthentication::where("u_id", $uid)->whereNull("delete_at")->where("status",1)->find();
        if(!empty($find)) return '已认证';
        return '未认证';
    }


    public static function getRegAuthUserList($param = [])
    {
        $order = ["a.id"=>"desc"];
        //按照创建时间排序
        if(!empty($param["order_ctime"])){
            if(!empty($param["order_ctime_sc"])){
                $order = ["a.id"=>"desc", "a.create_time"=> $param["order_ctime_sc"]];
            }else{
                //默认按照字段降序排序
                $order = ["a.id"=>"desc", "a.create_time"=>"desc"];
            }
        }

        $data['data'] = self::alias("a")
            ->field ("a.id as reg_id,a.uid,a.idcard,a.img,a.status,a.create_time,a.name,b.nickname,b.avatar,b.phone,FROM_UNIXTIME(a.create_time) as create_time,
            case when a.status=1 then '审核中' 
            when a.status=3 then '不通过' 
            when a.status=2 then '通过' 
            end as status_text 
            ")
            ->join ('user b','a.uid=b.uid','left')
            ->where(function ($query) use ($param){
                $query->whereNull("a.delete_time");
                if(!empty($param['time'])){    //根据创建日期搜索
                    $wheretime = explode (',',$param['time']);
                    $query->where('a.create_time', 'between', [strtotime($wheretime[0]),strtotime($wheretime[1])]);
                }
                if(!empty($param["word"])) $query->where("a.name",'like', '%'.$param["word"].'%');
            })
            ->order($order)
            ->page($param['page'], $param['limit'])
            ->select()->toArray();

        $data['count'] = self::alias("a")
            ->field ("a.id as reg_id,a.uid,a.idcard,a.img,a.status,a.create_time,a.name,b.nickname,b.avatar,b.phone")
            ->join ('user b','a.uid=b.uid','left')
            ->where(function ($query) use ($param){
                $query->whereNull("a.delete_time");
                if(!empty($param['time'])){    //根据创建日期搜索
                    $wheretime = explode (',',$param['time']);
                    $query->where('a.create_time', 'between', [strtotime($wheretime[0]),strtotime($wheretime[1])]);
                }
                if(!empty($param["word"])) $query->where("a.name",'like', '%'.$param["word"].'%');
            })
            ->count();

        if(!empty($data['data'] )) {
            foreach ($data['data'] as $key => &$val) {
                $img = explode(",", $val["img"]);
                $val["positive_img"] = $img[0];
                $val["side_img"] = $img[0];
            }
        }

        return $data;
    }



    public static function enableRegUserStatus($param = [])
    {
        $update = ['status' => $param["status"]];
        if(!empty($param["audit_desc"])) $update["audit_desc"] = $param["audit_desc"];

        $res = self::where("id", $param['id'])->update($update);
        return $res;
    }



    public static function getRegAuthUserDetail($param = [])
    {
        $detail = self::alias("a")
            ->field ("a.id as reg_id,a.uid,a.idcard,a.img,a.status,a.create_time,a.name,b.nickname,b.avatar,b.mobile,
            case when a.status=1 then '审核中' 
            when a.status=3 then '不通过' 
            when a.status=2 then '通过' 
            end as status_text 
            ")
            ->join ('user b','a.uid=b.id','left')
            ->where("a.id", $param["id"])
            ->find();

        return $detail;
    }


}